﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Media;
using WowSounds.Properties;
using Microsoft.Win32;

namespace WowSounds
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
            GetLocale();
            _Load();
            splitContainer1.Panel1Collapsed = true;
        }

        private DataTable SoundsTable = new DataTable();
        private String WowPach { get; set; }
        private String WowLocale { get; set; }
        private string soundEntry { get; set; }
        private string textEntry { get; set; }
        private string FileName { get; set; }

        private string BaseName
        {
            get
            {
                var Base = "";
                switch (_cbTextTable.Text)
                {
                    case "creature_ai_texts":
                        Base = Settings.Default.DBmangos;
                        break;
                    case "script_texts":
                    case "custom_texts":
                        Base = Settings.Default.DBscriptdev2;
                        break;
                }
                return String.Format("`{0}`.`{1}`", Base, _cbTextTable.Text);
            }
        }

        private void DataLoad()
        {
            SoundEntry se = new SoundEntry(_lvSounds, BaseName, GetWowDir, _cbLocales.Text, !_cbGetText.Checked);
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            SaveSettings();
        }

        private void _Load()
        {
            _tbUser.Text = Settings.Default.User;
            _tbPass.Text = Settings.Default.Pass;
            _tbDBmangos.Text = Settings.Default.DBmangos;
            _tbDBscriptdev2.Text = Settings.Default.DBscriptdev2;
            _cbTextTable.Text = Settings.Default.TextTable;

            splitContainer4.Panel1Collapsed = !Settings.Default.First;
            groupBox6.Text = "content locale " + _cbLocales.Text;
        }

        private void SaveSettings()
        {
            Settings.Default.User = _tbUser.Text;
            Settings.Default.Pass = _tbPass.Text;
            Settings.Default.DBmangos = _tbDBmangos.Text;
            Settings.Default.DBscriptdev2 = _tbDBscriptdev2.Text;
            Settings.Default.Locale = _cbLocales.Text;
            Settings.Default.TextTable = _cbTextTable.Text;
            Settings.Default.First = false;
            Settings.Default.Save();
        }

        private void _tsExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void _tsProperties_Click(object sender, EventArgs e)
        {
            if (!splitContainer4.Panel1Collapsed)
                SaveSettings();

            splitContainer4.Panel1Collapsed = !splitContainer4.Panel1Collapsed;
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            SaveSettings();
        }

        private void _bTestConnections_Click(object sender, EventArgs e)
        {

        }

        private void GetLocale()
        {
            if (GetWowDir == null)
            {
                MessageBox.Show("wow exists, please start wow.exe");
                return;
            }

            if (!Directory.Exists(GetWowDir + Settings.Default.Locale))
                return;

            var folders = Directory.GetDirectories(GetWowDir);
            if (folders.Length == 1)
            {
                Settings.Default.Locale = folders.First();
                _cbLocales.Text = folders.First().Replace(GetWowDir, "");
                _cbLocales.Enabled = false;
                groupBox6.Text = "content locale " + folders.First().Replace(GetWowDir, "");
            }
            else
            {
                foreach (var folder in folders)
                    _cbLocales.Items.Add(folder.Replace(GetWowDir, ""));

                _cbLocales.Enabled = true;
            }
 
        }

        private void _bOK_Click(object sender, EventArgs e)
        {
            splitContainer4.Panel1Collapsed = true;
            SaveSettings();
            DataLoad();
        }

        private void _cbTextTable_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_cbTextTable.Text != "" && _cbTextTable.SelectedIndex != -1)
            {
                DataLoad();
            }
            else
            {
                _cbGetText.Checked = false;
            }
            _cbGetText.ForeColor = _cbGetText.Checked ? Color.Red : Color.Blue;
        }

        private void _lvSounds_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_lvSounds.SelectedItems.Count > 0)
            {
                _rtbDef.Text = _lvSounds.SelectedItems[0].SubItems[3].Text;
                _rtbLoc.Text = _lvSounds.SelectedItems[0].SubItems[4].Text;
                soundEntry =   _lvSounds.SelectedItems[0].SubItems[0].Text;
                textEntry =    _lvSounds.SelectedItems[0].SubItems[1].Text;
                FileName =     _lvSounds.SelectedItems[0].SubItems[2].Text;

                groupBox2.Text = String.Format("Text: {0}   Sound: {1}", textEntry, soundEntry);
                PlaySoynds();
            }
        }

        private void PlaySoynds()
        {
            var patch = String.Format("{0}\\{1}\\", GetWowDir, _cbLocales.Text);
            Stream stream=null;

            MpqArchive arch1 = new MpqArchive(String.Format("{0}speech-{1}.MPQ",           patch, Settings.Default.Locale));
            MpqArchive arch2 = new MpqArchive(String.Format("{0}expansion-speech-{1}.MPQ", patch, Settings.Default.Locale));
            MpqArchive arch3 = new MpqArchive(String.Format("{0}lichking-speech-{1}.MPQ",  patch, Settings.Default.Locale));
            
            if (arch1.FileExists(FileName))
                stream = arch1.OpenFile(FileName);
            else if (arch2.FileExists(FileName))
                stream = arch2.OpenFile(FileName);
            else if (arch3.FileExists(FileName))
                stream = arch3.OpenFile(FileName);
            else
            {
                MessageBox.Show("File: " + FileName + " is exists!");
                arch1.Dispose();
                arch2.Dispose();
                arch3.Dispose();
                return;
            }

            SoundPlayer player = new SoundPlayer(stream);
            player.Play();

            stream.Close();
            stream.Dispose();
            arch1.Dispose();
            arch2.Dispose();
            arch3.Dispose();
        }

        private void _bToBase_Click(object sender, EventArgs e)
        {
            var text = _rtbLoc.Text.Replace("'", "\'");
            SoundEntry.ExecuteQuery(BaseName, text, textEntry);
            _lvSounds.SelectedItems[0].SubItems[4].Text = _rtbLoc.Text;
        }

        private void _bPlay_Click(object sender, EventArgs e)
        {
            PlaySoynds();
        }

        private void _cbLocales_SelectedIndexChanged(object sender, EventArgs e)
        {
            groupBox6.Text = "content locale " + _cbLocales.Text;
            Settings.Default.Locale = _cbLocales.Text;
        }

        private String GetWowDir
        {
            get
            {
                var patch = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Blizzard Entertainment\World of Warcraft", 
                    "InstallPath", "").ToString();
                
                return patch == "" ? null : patch+"Data\\"; 
            }
        }

        private void _cbTreeView_CheckedChanged(object sender, EventArgs e)
        {
            splitContainer1.Panel1Collapsed = !_cbTreeView.Checked;
            splitContainer2.Panel2Collapsed = _cbTreeView.Checked;
        }
    }
}
